#!/usr/bin/env node

// Node CLI 应用入口文件必须要有这样的文件头

const path = require("path");
const fs = require("fs");
const inquirer = require("inquirer");
const ejs = require("ejs");
const question = require("./question.js");

console.log(question);

inquirer.prompt(question).then(answers => {
    const temp = path.join(__dirname, "templates");
    const dest = process.cwd();

    fs.readdir(temp, (err, files) => {
        if (err) throw err;
        files.forEach(file => {
            console.log(file);
            ejs.renderFile(path.join(temp, file), answers, (err, str) => {
                if (err) throw err;
                let hasDir = fs.existsSync(path.join(dest, 'dist'))
                if (!hasDir) {
                    fs.mkdirSync("./dist");
                }
                fs.writeFileSync(path.join(dest, "dist", file), str);
            });
        });
    });
});
